package com.example.lirisheng.springbootfinalexperiment.service;

import com.example.lirisheng.springbootfinalexperiment.entity.PersonInformation;
import com.example.lirisheng.springbootfinalexperiment.entity.RecordTemperature;
import com.example.lirisheng.springbootfinalexperiment.entity.Users;
import com.example.lirisheng.springbootfinalexperiment.repositories.*;
import com.example.lirisheng.springbootfinalexperiment.util.RespBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class AdminService {

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    AuthoritiesRepository authoritiesRepository;

    @Autowired
    PersonInformationRepository personInformationRepository;

    @Autowired
    RecordTemperatureRepository recordTemperatureRepository;

    @Autowired
    UserRoleRepository userRoleRepository;

    @Autowired
    PasswordEncoder passwordEncoder;

   protected  final Logger logger = LoggerFactory.getLogger(AdminService.class);

    @Transactional(rollbackFor = Exception.class)
    public RespBean resetAdminPassword(Integer resId, String oldPassword, String newPassword) throws Exception{
        //验证密码是否正确

            Optional<Users> user1 = usersRepository.findById(resId);
            Users users = user1.get();
            if(!passwordEncoder.matches(oldPassword,users.getPassword())){
                return RespBean.error("旧的密码不对");
            }
//            if (logger.isDebugEnabled()){
//                logger.debug("residentEntity:"+residentEntity);
//            }
            //更改密码
            newPassword = new BCryptPasswordEncoder().encode(newPassword);
            newPassword="{bcrypt}"+newPassword;
            usersRepository.UpdatePasswordById(newPassword,resId);
            return RespBean.error("密码更新成功");

    }

    public Users findResidentByUsername(String username) {
        Users users = usersRepository.findByUsername(username);
          return  users;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteAccount(Long resId) throws  Exception{


            PersonInformation personInformation = personInformationRepository.findByResId(resId);
            List<RecordTemperature> recordTemperatures = recordTemperatureRepository.findByResId(resId);
            if(personInformation!=null){
                personInformationRepository.delete(personInformation);
            }
            if(recordTemperatures!=null){
                recordTemperatureRepository.deleteAll(recordTemperatures);
            }
            userRoleRepository.deleteAllByUser(resId);

    }

    public  Map<String,Object> getWarn(){
        Map<String,Object> map = new LinkedHashMap<>();
        List<PersonInformation> entities1= personInformationRepository.findByWarnBetween(1,4);
        List<PersonInformation> entities2= personInformationRepository.findByWarnGreaterThanEqual(5);
        map.put("warn",entities1.size());
        map.put("serwarn",entities2.size());
        return  map;
    }

    public  List<PersonInformation> getResPersonInfo() {

        List<PersonInformation> personInformations=new ArrayList<PersonInformation>();
        Iterator<PersonInformation> iterator = personInformationRepository.findAll().iterator();
        iterator.forEachRemaining(personInformations::add);
        return personInformations;
    }


}
